package com.yxw.job;

import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;

import javax.annotation.PostConstruct;
import java.io.File;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.TimeZone;

@Slf4j
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients(basePackages = {
        "com.yxw.live_vod_facade.feign","com.yxw.live_vod_backend_facade.feign",
        "com.yxw.job.feign.client"
})
public class XxlJobApplication {

    private static String AppCurrenDir = "";

    public static String getAppCurrenDir() {
        return AppCurrenDir;
    }

    /**
     * 设置系统默认时区"Asia/Shanghai"
     */
    @PostConstruct
    public void init() {
        TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai")); // 设置默认时区
        log.info("默认时区为: ====================" + TimeZone.getDefault().getID());
    }

    public static void main(String[] args) {
        TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
        ConfigurableApplicationContext application = SpringApplication.run(XxlJobApplication.class, args);
        Environment env = application.getEnvironment();
        String ip;
        try {
            ip = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            ip = "";
        }
        String port = env.getProperty("server.port");
        String contextPath = env.getProperty("server.servlet.context-path");
        contextPath = null == contextPath ? "" : contextPath;
        File oFile = new File(com.yxw.job.XxlJobApplication.class.getProtectionDomain().getCodeSource().getLocation().getFile());
        AppCurrenDir = oFile.getParent().replaceAll("file:\\\\", "").replaceAll("\\\\live-manage-backend-1.0.0-SNAPSHOT.jar!\\\\BOOT-INF", "");
        log.info("--------------------------------------------------------------------------------------------------------------------\n\t" +
                "直播管理后台服务 is running! Access URLs:\n\t" +
                "Local: \t\thttp://localhost:" + port + contextPath + "/\n\t" +
                "Swagger文档: \thttp://" + ip + ":" + port + contextPath + "/doc.html\n\t" +
                "AppCurrenDir:" + AppCurrenDir + "\n\t" +
                "==========================================xxl-job服务启动完成==========================================");
    }
}
